package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbType;
import com.bwie.service.TbTypeService;
import com.bwie.mapper.TbTypeMapper;
import com.bwie.utils.Result;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【tb_type(分类表)】的数据库操作Service实现
* @createDate 2025-05-24 10:22:03
*/
@Service
public class TbTypeServiceImpl extends ServiceImpl<TbTypeMapper, TbType>
    implements TbTypeService{

    @Autowired
    TbTypeMapper tbTypeMapper;
    @Autowired
    HttpServletRequest request;
    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Override
    public Result addType(TbType tbType) {

        String token = request.getHeader("token");
        if(token == null){
            return Result.error().setMsg("请先登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String nickName = body.get("nickName", String.class);

        TbType type = tbTypeMapper.selectOne(new LambdaQueryWrapper<TbType>().eq(TbType::getTypeName, tbType.getTypeName()));
        if(type != null){
            return Result.error().setMsg("该类型已存在");
        }

        tbType.setCreateTime(new Date());
        tbType.setCreateBy(nickName);

        tbTypeMapper.insert(tbType);
        return Result.ok();
    }

    @Override
    public Result deleteByTypeId(Integer typeId) {
        TbType tbType = tbTypeMapper.selectById(typeId);
        if(tbType == null){
            return Result.error().setMsg("分类不存在");
        }

        tbTypeMapper.deleteById(typeId);
        return Result.ok();
    }

    @Override
    public Result getTypeTreeList() {
        List<TbType> tbTypes = tbTypeMapper.selectList(null);

        List<Tree<String>> treeList = TreeUtil.build(tbTypes, "0", (tree, type) -> {
            type.setId(tree.getTypeId() + "");
            type.setName(tree.getTypeName());
            type.setParentId(tree.getTypePid() + "");
            type.putExtra("typeImage",tree.getTypeImage());
            type.putExtra("typeStatus",tree.getTypeStatus());
            type.putExtra("typeTop",tree.getTypeTop());
        });
        return Result.ok().setData(treeList);
    }

    @Override
    public Result showAndHide(TbType tbType) {
        TbType type = tbTypeMapper.selectById(tbType.getTypeId());
        if(type == null){
            return Result.error().setMsg("该类型不存在");
        }

        if(type.getTypeStatus()==0){
            type.setTypeStatus(1);
            tbTypeMapper.updateById(type);
        }else if(type.getTypeStatus() == 1){
            type.setTypeStatus(0);
            tbTypeMapper.updateById(type);
        }
        return Result.ok();
    }

    @Override
    public Result loadImage(MultipartFile file) {

        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());

            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url = "http://192.168.80.192:8888/"+storePath.getFullPath();
            return Result.ok().setData(url);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error().setMsg("上传失败");
        }
    }

    @Override
    public Result updateType(TbType tbType) {
        TbType type = tbTypeMapper.selectById(tbType.getTypeId());
        if(type == null){
            return Result.error().setMsg("商品类型不存在");
        }

        String token = request.getHeader("token");
        if(token == null){
            return Result.error().setMsg("请先登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String nickName = body.get("nickName", String.class);

        tbType.setUpdateBy(nickName);
        tbType.setUpdateTime(new Date());
        tbTypeMapper.updateById(tbType);
        return Result.ok();
    }

    @Override
    public Result getTypeSelectTree() {
        List<TbType> tbTypes = tbTypeMapper.selectList(null);

        List<Tree<String>> treeList = TreeUtil.build(tbTypes, "0", (tree, type) -> {
            type.setId(tree.getTypeId() + "");
            type.setName(tree.getTypeName());
            type.setParentId(tree.getTypePid() + "");
            type.putExtra("label",tree.getTypeName());
            type.putExtra("value",tree.getTypeId());
            type.putExtra("typeImage",tree.getTypeImage());
            type.putExtra("typeStatus",tree.getTypeStatus());
            type.putExtra("typeTop",tree.getTypeTop());
        });
        return Result.ok().setData(treeList);
    }
}




